import os
import sys

from PySide6.QtCore import Qt, QSettings, QByteArray
from PySide6.QtGui import QIcon, QGuiApplication, QAction
from PySide6.QtWidgets import (QApplication, QMainWindow, QMenuBar, QMessageBox,
                             QTextEdit, QToolBar, QVBoxLayout, QWidget)

from . import images_rc
from .utils import R
from .core.board import Board, AiBoard

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        w = (R.BoardSize+1)*R.StoneSize + 100
        self.resize(w, w)
        screen = QGuiApplication.primaryScreen().availableGeometry()
        size = self.geometry()
        self.move(int((screen.width()-size.width())/2), 60)
        self.setWindowTitle("x01 - " + R.AppName)

        # self.setStyleSheet('background-color: rgb(230, 230, 80);')
        self.board = Board()
        self.setCentralWidget(self.board)

        self.InitSettings()
        self.InitActions()
        self.InitMenu()
        self.InitToolBar()

        self.filename = ''

    def closeEvent(self, e):
        ''' override closeEvent for settings '''
        settings = QSettings('x01', R.AppName)
        settings.setValue('filename', self.filename)
        settings.setValue("MainWindow/Geometry", self.saveGeometry())
        settings.setValue("MainWindow/State", self.saveState())

    def InitSettings(self):
        settings = QSettings('x01', R.AppName)
        if settings.value('filename'):
            self.filename = settings.value('filename')
        if settings.value("MainWindow/Geometry"):
            self.restoreGeometry(
                QByteArray(settings.value("MainWindow/Geometry")))
        if settings.value("MainWindow/State"):
            self.restoreState(QByteArray(settings.value("MainWindow/State")))

    def InitActions(self):
        self.StartGameAction = QAction(QIcon(':images/new_file.gif'),
            '&Start Game', self, shortcut='Ctrl+N', triggered=self.StartGame)
        self.AiGameAction = QAction(QIcon(':images/open_file.gif'),
            '&Ai Game', self, triggered=self.AiGame, shortcut='Ctrl+O')
        self.GameActions = [self.StartGameAction, self.AiGameAction]

        self.helpAction = QAction("&Help", triggered=self.Help)
        self.aboutAction = QAction("&About", triggered=self.About)
        self.helpActions = [self.helpAction, self.aboutAction]

    def InitToolBar(self):
        self.GameBar = QToolBar('Game toolbar', self)
        self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.GameBar)
        R.AddActions(self.GameBar, self.GameActions)

    def InitMenu(self):
        self.menuBar = QMenuBar(self)
        self.setMenuBar(self.menuBar)

        self.menuFile = self.menuBar.addMenu(self.tr('Game'))
        R.AddActions(self.menuFile, self.GameActions)

        self.menuHelp = self.menuBar.addMenu(self.tr('Help'))
        R.AddActions(self.menuHelp, self.helpActions)


    def StartGame(self):
        self.board = Board()
        self.setCentralWidget(self.board)

    def AiGame(self):
        self.board = AiBoard()
        self.setCentralWidget(self.board)

    def Help(self): 
        QMessageBox.about(
            self, R.AppName, '略'
        )

    def About(self):
        QMessageBox.about(
            self, R.AppName,
            f'{R.AppName} 版本 {R.AppVersion}\nCopyright (c)2023 by x01(Huangxiong). All rights reserved.\n版权属于黄雄(x01)，所有权利保留。'
        )


def run():
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec())


if __name__ == '__main__':
    run()  # 包内不可运行
